from django.core.validators import ValidationError


def validate_image_size(value):
    max_size = 2 * 1024 * 1024  # 2MB
    if value.size > max_size:
        raise ValidationError("图片大小不能超过2MB")

    # 检查图片尺寸
    from PIL import Image

    img = Image.open(value.file)
    if img.width > 1024 or img.height > 1024:
        raise ValidationError("图片尺寸不能超过1024x1024像素")
